﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class RepairCycAdd : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(RepairCycAdd));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        //public string sFileName = GlobleClass.UpFileType.DevPartfile.ToString();//"Devfile";

        #region 载入信息

        public void loadInfo()
        {
            DeviceInfo deviceinfo = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                deviceinfo = new DeviceInfo();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(173, ref sModelName, (clsBaseLib)deviceinfo))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(172, ref sModelName, (clsBaseLib)deviceinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(174, ref sModelName, (clsBaseLib)deviceinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = deviceinfo.getDevRepairCycInfoById(m_Id);
                    //hdselect.Value = buy.getDeviceSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtMaintainCode.Text = Convert.ToString(dt.Rows[0]["PlanCode"]);
                        txtMaintainCode.Enabled = false;
                        txtDevID.Value = Convert.ToString(dt.Rows[0]["DevID"]);
                        lbDevCode.Text = Convert.ToString(dt.Rows[0]["DeviceCode"]);
                        Button_Dev.Visible = false;
                        lbDevName.Text = Convert.ToString(dt.Rows[0]["DeviceName"]);
                        lbUesDept.Text = Convert.ToString(dt.Rows[0]["DeptName"]);
                        lbDevSpec.Text = Convert.ToString(dt.Rows[0]["Devicespec"]);

                        if (dt.Rows[0]["dRepairLastTime"] != null && dt.Rows[0]["dRepairLastTime"].ToString().Trim() != "")
                        {
                            txtBeginDate.Text = Convert.ToDateTime(dt.Rows[0]["dRepairLastTime"]).ToString("yyyy-MM-dd");
                        }
                        if (dt.Rows[0]["dRepairNextTime"] != null && dt.Rows[0]["dRepairNextTime"].ToString().Trim() != "")
                        {
                            txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["dRepairNextTime"]).ToString("yyyy-MM-dd");
                        }

                        txtCyc.Text = Convert.ToString(dt.Rows[0]["RepairCyc"]);
                        DropDownList_CycUnit.SelectedValue = Convert.ToString(dt.Rows[0]["RepairDanwei"]);
                        DropDownList_Level.SelectedValue = Convert.ToString(dt.Rows[0]["RepairType"]);
                        TextBoxZhaiyao.Text = Convert.ToString(dt.Rows[0]["RepairNeed"]);
                        
                        txtComment.Text = Convert.ToString(dt.Rows[0]["comment"]);
                    }

                    #region //获取维修计划维修项目列表
                    dt = ds.Tables[1];
                    int iRowCount = dt.Rows.Count;
                    StringBuilder sbu = new StringBuilder();
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<tr>
                        <td align=""left"" width=""120"">
                            <a href=""javascript:"" onclick=""insertItem(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 64px"" type=""hidden"" value=""{1}"" /><input style=""width: 92px"" type=""text"" value=""{0}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 150px"" type=""text"" value=""{2}"" /></td>
                        <td width=""70"">
                            <input  style=""width: 150px"" type=""text"" value=""{3}"" /></td>
                    </tr>", dt.Rows[i]["ItemName"], dt.Rows[i]["ID"], dt.Rows[i]["ItemNeed"], dt.Rows[i]["comment"]));
                    }
                    ltrPlanItem.Text = Convert.ToString(sbu);
                    #endregion
                }
            }
//            else
//            {
//                ltrPlanItem.Text = @"<tr>
//                        <td align=""left"" width=""120"">
//                            <a href=""javascript:"" onclick=""insertItem(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removeItem(this.parentNode.parentNode)"">-</a><input style=""width: 64px"" type=""hidden"" value=""0"" /><input style=""width: 92px"" type=""text"" /></td>
//                        <td width=""70"">
//                            <input style=""width: 150px"" type=""text"" /></td>
//                        <td width=""70"">
//                            <input style=""width: 150px"" type=""text"" /></td>
//                    </tr>";
//            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        //[Ajax.AjaxMethod]
        //public DataTable getSelectDeviceInfo(string sIds)
        //{
        //    BuyDevice buy = null;
        //    DataTable dt = null;
        //    try
        //    {
        //        buy = new BuyDevice();
        //        dt = buy.getResultDeviceInfo(sIds);
        //    }
        //    catch
        //    {
        //        dt = null;
        //    }
        //    finally
        //    {
        //        if (buy != null)
        //        {
        //            buy.Dispose();
        //        }
        //    }
        //    return dt;
        //} 

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int maintainid, string maintaincode, int devid, string maintaintype, int maintaincyc, string cycunit, string begindate, string enddate, string zhaiyao, string comment, string[] Items)
        {
            DeviceInfo devinfo = null;
            bool bResult = false;
            maintaincode = maintaincode.Replace("'", "''");
            zhaiyao = zhaiyao.Replace("'", "''");
            comment = comment.Replace("'", "''");
            //spartspec = spartspec.Replace("'", "''");
            //spartweight = spartweight.Replace("'", "''");

            //spartcailiao = spartcailiao.Replace("'", "''");
            //spartfactory = spartfactory.Replace("'", "''");
            //spartprice = spartprice.Replace("'", "''");
            //spartapplydev = spartapplydev.Replace("'", "''");
            //spartcomment = spartcomment.Replace("'", "''");
            try
            {
                devinfo = new DeviceInfo();

                //for (int i = 0; i < sDevices.Length; i++)
                //{
                //    sDevices[i] = sDevices[i].Replace("'", "''");
                //}
                bResult = devinfo.setRepairCycInfo(maintainid, maintaincode, devid, maintaintype, maintaincyc, cycunit, begindate, enddate, zhaiyao, comment,Items);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (devinfo != null)
                {
                    devinfo.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}